package com.sskj.mine.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.HideUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.MineInviteActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.AddressListActivity;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.invite.InviteActivity;
import com.sskj.mine.presenter.MineFragmentPresenter;
import com.sskj.mine.ui.activity.FeedbackActivity;
import com.sskj.mine.ui.activity.PersonActivity;
import com.sskj.mine.ui.activity.SecurityActivity;
import com.sskj.mine.ui.activity.SettingActivity;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_FRAGMENT_MAIN)
public class MineFragment extends BaseFragment<MineFragmentPresenter> {

    @BindView(R2.id.user_head_img)
    ImageView userHeadImg;
    @BindView(R2.id.mine_message)
    ImageView mineFeedback;
    @BindView(R2.id.mine_setting)
    ImageView mineSetting;
    @BindView(R2.id.name_tv)
    TextView nameTv;


    @BindView(R2.id.user_slogan)
    TextView userSlogan;
    @BindView(R2.id.user_id)
    TextView userId;
    @BindView(R2.id.uid_tv)
    TextView uid_tv;

    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    @BindView(R2.id.tvUsdt)
    TextView tvUsdt;
    @BindView(R2.id.tvBTC)
    TextView tvBTC;
    @BindView(R2.id.ll_user_name)
    LinearLayout ll_user_name;

    @BindView(R2.id.tv_simple_user)
    TextView tvSimpleUser;

    @BindView(R2.id.iv_vip)
    ImageView iv_vip;

    @BindView(R2.id.tv_charge_coin)
    TextView tvChargeCoin;
    @BindView(R2.id.tv_pickup_coin)
    TextView tvPickUpCoin;
    @BindView(R2.id.tv_my_invite)
    TextView tvMyInvite;
    @BindView(R2.id.tv_help_center)
    TextView tvHelpCenter;
    @BindView(R2.id.menu_personal_center)
    SuperTextView menuPersonalCenter;
    @BindView(R2.id.menu_security_center)
    SuperTextView menuSecurityCenter;
    @BindView(R2.id.menu_asset_management)
    SuperTextView menuAssetManagement;
    @BindView(R2.id.menu_pick_coin_address)
    SuperTextView menuPickCoinAddress;
    @BindView(R2.id.menu_contact_us)
    SuperTextView menuContractGuest;
    @BindView(R2.id.menu_about_us_1)
    SuperTextView menuAboutUsNew;
    @BindView(R2.id.menu_feedback)
    SuperTextView menuContactUs;
    @BindView(R2.id.rl_invite)
    RelativeLayout rlInviteFriend;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.mine_fragment_mine;
    }

    @Override
    public MineFragmentPresenter getPresenter() {
        return new MineFragmentPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        DaggerUserDataComponent.create().inject(this);
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                Flowable.timer(2, TimeUnit.SECONDS)
                        .compose(RxSchedulersHelper.transformer())
                        .subscribe(aLong -> {
                            refreshView.setRefreshing(false);
                        });
                userViewModel.update();
            }
        });
        updateUserTitle();
        tvMyInvite.setVisibility(Constans.isInvite ? View.VISIBLE : View.GONE);
        rlInviteFriend.setVisibility(Constans.isInvite ? View.VISIBLE : View.GONE);


    }

    @Override
    public void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);

            } else {
                userData = null;
            }
            updateUserTitle();
        });
    }

    @Override
    protected void initEvent() {


        ClickUtil.click(nameTv, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
        });

        ClickUtil.click(mineFeedback, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            FeedbackActivity.start(getContext());
        });

        ClickUtil.click(mineSetting, () -> {
            SettingActivity.start(getContext());
        });

        ClickUtil.click(tvChargeCoin, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }

            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }

            ARouter.getInstance().build(RConfig.MINE_RECHARGE).navigation();
        });

        ClickUtil.click(tvPickUpCoin, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }

            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_WITHDRAW).navigation();
        });

        ClickUtil.click(tvMyInvite, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            InviteActivity.start(getContext());
        });

        ClickUtil.click(tvHelpCenter, () -> {
            ARouter.getInstance().build(RConfig.HANG_GUIDE_WEB)
                    .withBoolean(Constans.IS_HELP, true)
                    .navigation();
        });


        ClickUtil.click(rlInviteFriend, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            MineInviteActivity.start(getContext());
        });


        ClickUtil.click(menuPersonalCenter, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_PERSON).navigation();
        });

        ClickUtil.click(menuSecurityCenter, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_SECURITY_CENTER).navigation();
        });

        ClickUtil.click(menuAssetManagement, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.TIBI_ASSET_MANAGE).navigation();
        });

        ClickUtil.click(menuPickCoinAddress, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            AddressListActivity.start(getContext(), 2);

        });

        ClickUtil.click(menuContractGuest, () -> {

            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.HANG_GUIDE_WEB).withBoolean(Constans.IS_GUEST, true).navigation();
        });

        ClickUtil.click(menuAboutUsNew, () -> {
            ARouter.getInstance().build(RConfig.HANG_GUIDE_WEB).withBoolean(Constans.IS_ABOUT_US, true).navigation();
        });

        ClickUtil.click(menuContactUs, () -> {
            mPresenter.getContract();
        });


    }

    public void updateUserTitle() {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        if (userData != null) {
            nameTv.setText(SPUtil.get(SPConfig.USER_ACCOUNT, "").contains("@") ? HideUtil.getEmialAddress(SPUtil.get(SPConfig.USER_ACCOUNT, "")) : HideUtil.getPhoneHide(SPUtil.get(SPConfig.USER_ACCOUNT, "")));
            uid_tv.setText("UID: " + userData.getUserUid());


            String vipLevel = userData.getVipLevel() + "";
            if (!TextUtils.isEmpty(vipLevel)) {
                tvSimpleUser.setVisibility(View.GONE);
                switch (vipLevel) {
                    case "1":
                        iv_vip.setImageResource(R.mipmap.mine_icon_v1);
                        break;
                    case "2":
                        iv_vip.setImageResource(R.mipmap.mine_icon_v2);
                        break;
                    case "3":
                        iv_vip.setImageResource(R.mipmap.mine_icon_v3);
                        break;
                    case "4":
                        iv_vip.setImageResource(R.mipmap.mine_icon_v4);
                        break;
                    case "5":
                        iv_vip.setImageResource(R.mipmap.mine_icon_v5);
                        break;
                    case "6":
                        iv_vip.setImageResource(R.mipmap.mine_icon_v6);
                        break;
                    default:
                        tvSimpleUser.setVisibility(View.VISIBLE);
                        iv_vip.setVisibility(View.GONE);
                        break;
                }
            } else {
                tvSimpleUser.setVisibility(View.VISIBLE);
                iv_vip.setVisibility(View.GONE);
            }
            tvBTC.setText(userData.getFound() != null ? userData.getFound().getUsdtTotal() : "----");
            tvUsdt.setText(userData.getFound() != null ? "≈" + CommonUtil.dealDanwei(userData.getFound().getUsdtTotal()) + " " + Constans.DANWEI : "");
            userSlogan.setText(userData.getVipLevel() + "");
            if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
                coolRefreshView.setRefreshing(false);
            }

        } else {
            iv_vip.setVisibility(View.GONE);
            nameTv.setText(App.INSTANCE.getString(R.string.minemineFragment1));
            uid_tv.setText("--");

            tvBTC.setText("----");
            tvUsdt.setText("----");
            userSlogan.setText("----");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (userViewModel != null) {
            userViewModel.update();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (userViewModel != null) {
                userViewModel.update();
            }
        }
    }


    public void showEmail(String email) {
        TipUtil.getSureTip(getActivity(), ""/*App.INSTANCE.getString(R.string.minemineFragment2)*/, email, App.INSTANCE.getString(R.string.minemineFragment3), () -> {
            CopyUtil.copy(email);
        });

    }


}
